package com.person.ntv.controller.sys;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.Page;
import com.person.common.InitApplication;
import com.person.common.PageParams;
import com.person.exception.ServiceException;
import com.person.ntv.entity.sys.SysCode;
import com.person.ntv.entity.sys.SysCodeset;
import com.person.ntv.service.sys.CommonService;
import com.person.ntv.service.sys.SysCodeService;
import com.person.ntv.service.sys.SysCodeSetService;

@Controller
@RequestMapping("/system/sysCode")
public class SysCodeController {
	    @Autowired
		private SysCodeService  sysCodeService ;
	    @Autowired
		private SysCodeSetService  sysCodeSetService ;
	    @Autowired
		private CommonService commonService ;
		@RequestMapping("/listPage")
		public String  getCodePage(ModelMap mv)throws Exception{
			initCode(mv);
			return "/system/code/codeList";
		}
		
		@RequestMapping("/codeSetList")
		@ResponseBody
		public List<Map<String,Object>> getCodeSetList(String codeSet,HttpServletRequest request) throws Exception{
			List<SysCodeset> codeSetList=sysCodeSetService.selectAll();
			List<Map<String,Object>>list=new ArrayList<Map<String,Object>>();
			if(codeSetList!=null)
			for(SysCodeset cs:codeSetList){
				if(StringUtils.isBlank(codeSet)||cs.getCnName().indexOf(codeSet)!=-1||cs.getEnName().indexOf(codeSet)!=-1){
					Map<String,Object> map=new HashMap<String,Object>();
					map.put("id", cs.getId());
					map.put("value",cs.getId());
					map.put("text",cs.getCnName()+"("+cs.getEnName()+")");
					list.add(map);
				}
			}
			return list;
		}	
		
		@RequestMapping("/codeList")
		@ResponseBody
		public Map<String,Object> getCodeList(PageParams myPage) throws Exception{
			Map<String,Object> map=new HashMap<String,Object>();
			Page<SysCode> page=sysCodeService.getCodePage(myPage);
	        map.put("total",page.getTotal());
	        map.put("rows",page.getResult());
			return map;
		}
		
		@RequestMapping("/addPage")
		public String  addCodePage(ModelMap mv,String csId) throws Exception{
			initCode(mv);
			mv.addAttribute("csId", csId);
			return "/system/code/codeAdd";
		}
		
		@RequestMapping("/editPage")
		public String  editCodePage(String id,ModelMap mv) throws Exception{
			try {
			initCode(mv);
		    SysCode code =sysCodeService.selectByPrimaryKey(new BigDecimal(id));
		    mv.addAttribute("code",code);
			} catch (ServiceException e) {
				mv.addAttribute("error", "1");
				mv.addAttribute("msg",e.getMessage()+"查询出错！");
			}
			return "/system/code/codeEdit";
		}
		
		@RequestMapping("/addCode")
		public String  addCode(ModelMap mv,SysCode sysCode) throws Exception{
			try {
				sysCodeService.insertSelective(sysCode);
				InitApplication.codeMap=codeSetList();
				mv.addAttribute("error", "0");
				mv.addAttribute("msg","保存成功！");
			} catch (ServiceException e) {
				mv.addAttribute("error", "1");
				mv.addAttribute("msg",e.getMessage()+"保存失败！");
			}
			return "/system/code/codeAdd";
		}
		
		@RequestMapping("/editCode")
		public String  editCode(ModelMap mv,SysCode sysCode) throws Exception{
			try {
				sysCodeService.updateByPrimaryKey(sysCode); 
				InitApplication.codeMap=codeSetList();
				mv.addAttribute("error", "0");
				mv.addAttribute("msg","保存成功！");
			} catch (ServiceException e) {
				mv.addAttribute("error", "1");
				mv.addAttribute("msg",e.getMessage()+"保存失败！");
			}
			return "/system/code/codeEdit";
		}
		
		@RequestMapping("/delCode")
		@ResponseBody
		public Map<String,Object>  delCode(BigDecimal[]ids,HttpServletRequest request) throws Exception{
			Map<String,Object> map=new HashMap<String,Object>();
			try {
				sysCodeService.deleteByPrimaryKeys(ids); 
				InitApplication.codeMap=codeSetList();
				map.put("error", "0");
				map.put("msg","删除成功！");
			} catch (ServiceException e) {
				map.put("error", "1");
				map.put("msg",e.getMessage()+"删除失败！");
			}
			return map;
		}
		
		@RequestMapping("/detailPage")
		public String  detailCodeSetPage(String id,ModelMap mv) throws Exception{
			try {
		    SysCode code = sysCodeService.selectByPrimaryKey(new BigDecimal(id));
		    mv.addAttribute("code",code);
			} catch (ServiceException e) {
				mv.addAttribute("error", "1");
				mv.addAttribute("msg",e.getMessage()+"查询出错！");
			}
			return "/system/code/codeDetail";
		}
		
		@RequestMapping("/validateItemName")
		@ResponseBody
		public boolean validateItemName(SysCode sysCode) throws Exception{
			try {
				if(sysCode.getItemName()==null)sysCode.setItemName("");
				int cnt=sysCodeService.validateItemName(sysCode.getItemName(), sysCode.getId()==null?null:String.valueOf(sysCode.getId()),String.valueOf(sysCode.getCsId()));
				if(cnt==0)
				return true;
			} catch (ServiceException e) {
	           e.printStackTrace();
			}
			return false;
		}
		
		@RequestMapping("/validateItemValue")
		@ResponseBody
		public boolean validateItemValue(SysCode  sysCode) throws Exception{
			try {
				if(sysCode.getItemValue()==null)sysCode.setItemValue("");
				int cnt=sysCodeService.validateItemValue(sysCode.getItemValue(), sysCode.getId()==null?null:String.valueOf(sysCode.getId()),String.valueOf(sysCode.getCsId())); 
				if(cnt==0)
				return true;
			} catch (ServiceException e) {
				e.printStackTrace();
			}
			return false;
		}
		
		private void initCode(ModelMap mv){
			mv.addAttribute("SysCodeStatus",JSONArray.fromObject(commonService.getCodeList("SysCodeStatus",true)).toString());
		}
		
		private  Map<String,List<SysCode>>codeSetList()throws Exception {
			List<Map<String,Object>> list = commonService.getCodeSetList();
			Map<String,List<SysCode>> data=new HashMap<String,List<SysCode>>();
			SysCode code=null;
			List<SysCode> codeList=null;
			for(Map<String,Object>map:list){
				codeList=data.get(String.valueOf(map.get("enName")));
				if(codeList==null){
					codeList=new ArrayList<SysCode>();
				}
				code=new SysCode();
				code.setId(new BigDecimal(String.valueOf(map.get("id"))));
				code.setItemName(String.valueOf(map.get("itemName")).trim());
				code.setItemValue(String.valueOf(map.get("itemValue")).trim());
				code.setParentItemId(String.valueOf(map.get("parentItemId")));
				codeList.add(code);
				data.put(String.valueOf(map.get("enName")),codeList);
			}
			return data;
		}
}
